package com.grasp.base;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.grasp.activity.R;
import com.grasp.data.IconItem;

public class CustomGridViewAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<IconItem> mItems;

	public CustomGridViewAdapter(Context context, List<IconItem> items) {
		inflater = LayoutInflater.from(context);
		mItems = items;
	}

	@Override
	public int getCount() {
		return mItems.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mItems.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ItemHolder mHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.gridview_item, null);
			mHolder = new ItemHolder();
			mHolder.iconView = (ImageView) convertView
					.findViewById(R.id.gridview_item_icon);
			mHolder.selectView = (ImageView) convertView
					.findViewById(R.id.gridview_item_select);
			mHolder.titleView = (TextView) convertView
					.findViewById(R.id.gridview_item_title);
			convertView.setTag(mHolder);
		} else {
			mHolder = (ItemHolder) convertView.getTag();
		}
		IconItem item = mItems.get(position);
		mHolder.titleView.setText(item.getTitle());
		int id = parent
				.getContext()
				.getResources()
				.getIdentifier(item.getFileName(), "drawable",
						parent.getContext().getPackageName());
		mHolder.iconView.setImageResource(id);
		// mHolder.selectView.setVisibility(item.isSelect() ? View.VISIBLE
		// : View.GONE);
		return convertView;
	}

	class ItemHolder {
		private ImageView iconView;
		private ImageView selectView;
		private TextView titleView;
	}

}
